using System;
using System.Collections.Generic;
using System.Text;

namespace TaugeLibrary.Classes
{

    public class LookupParameter
    {
        public string Sql;
        public string ReturnColumn;
        public string LookupColumn;
        public string Title;
        public LookupParameter(string p_sql, string p_return_column, string p_lookup_column, string p_title)
        {
            Sql = p_sql;
            ReturnColumn = p_return_column;
            LookupColumn = p_lookup_column;
            Title = p_title;
        }
    }

    public class LookupParameterTextBox
    {
        public LookupParameter LookupParam;
        public string SqlDescription;
        public LookupParameterTextBox(LookupParameter lp, string p_SqlDescription)
        {
            LookupParam = lp;
            SqlDescription = p_SqlDescription;
        }
    }
    /*
    public class SearchCriteria
    {
        //private int m_seq;
        private string m_logical;
        private string m_field;
        private string m_operator;
        private string m_value;

        //public int Seq
        //{
        //    set { m_seq = value; }
        //    get { return m_seq; }
        //}

        public string Logical
        {
            set { m_logical = value; }
            get { return m_logical; }
        }
        public string Field
        {
            set { m_field = value; }
            get { return m_field; }
        }
        public string Operator
        {
            set { m_operator = value; }
            get { return m_operator; }
        }
        public string Value
        {
            set { m_value = value; }
            get { return m_value; }
        }

        public SearchCriteria()
            : this("", "", "", "")
        {
        }

        public SearchCriteria(string p_logical, string p_field, string p_operator, string p_value)
        {
            m_logical = p_logical;
            m_field = p_field;
            m_operator = p_operator;
            m_value = p_value;
        }

    }

    public class BusinessUnitSearch
    {
        public static string getSqlSearchCondition(SearchCriteria[] p_criteria, BusinessUnitField[] p_bu_field)
        {
            string result = "";
            double tmpdbl = 0;
            //bool tmpbool = false;
            string tmpstr = "";
            DateTime tmpdate;
            TaugeDateTime tmphrdate;
            string op = "";
            foreach (SearchCriteria ct in p_criteria)
            {
                if (ct.Operator != "")
                {

                    if (ct.Operator == "==")
                        result += " " + ct.Logical + " " + ct.Field + " = " + ct.Value;
                    else if (ct.Operator == "!!")
                        result += " " + ct.Logical + " " + ct.Field + " != " + ct.Value;
                    else
                    {
                        foreach (BusinessUnitField fld in p_bu_field)
                        {

                            if (ct.Field.ToUpper() == fld.FieldDesc.ToUpper() || ct.Field.ToUpper() == fld.FieldDB.ToUpper())
                            {
                                if ("=,<,<=,>,>=,!=,<>".IndexOf(ct.Operator) >= 0)
                                {
                                    op = ct.Operator;
                                    if (op == "<>")
                                        op = "!=";
                                    switch (fld.FieldType)
                                    {
                                        case eFieldType.StringType:
                                            tmpstr = ct.Value; //.ToString();
                                            if (op == "=")
                                                result += " " + ct.Logical + " " + fld.FieldDB + " LIKE '%" + tmpstr.Replace("'", "''") + "%'";
                                            else
                                                result += " " + ct.Logical + " " + fld.FieldDB + " " + op + " " + LibConstants.sqlStr(tmpstr);
                                            break;
                                        case eFieldType.NumericType:
                                            //tmpdbl = (double)ct.Value;
                                            if (Double.TryParse(ct.Value, System.Globalization.NumberStyles.Number, null, out tmpdbl))
                                                result += " " + ct.Logical + " " + fld.FieldDB + op + tmpdbl.ToString();
                                            break;
                                        case eFieldType.DateType:
                                            tmphrdate = new TaugeDateTime();
                                            tmphrdate.DateText = ct.Value;
                                            tmpdate = tmphrdate.Value;
                                            if (tmpdate == DateTime.MinValue)
                                            {
                                                if (op == "=")
                                                    result += " " + ct.Logical + " " + fld.FieldDB + " IS NULL";
                                                else
                                                    result += " " + ct.Logical + " " + fld.FieldDB + " IS NOT NULL";
                                            }
                                            else
                                            {
                                                result += " " + ct.Logical + " dbo.f_YYYYMMDD(" + fld.FieldDB + ")" + op + "'" + tmpdate.ToString("yyyyMMdd") + "'";
                                            }
                                            break;
                                        case eFieldType.BooleanType:
                                            tmpstr = ct.Value; //.ToString();
                                            if ("0|1|Y|N|T|F".IndexOf(tmpstr.ToUpper().Substring(0, 1)) >= 0)
                                            {
                                                string tmp = "0";
                                                if ("1|Y|T".IndexOf(tmpstr.ToUpper().Substring(0, 1)) >= 0)
                                                    tmp = "1";

                                                result += " " + ct.Logical + " " + fld.FieldDB + op + tmp;
                                            }
                                            break;
                                        default:
                                            throw new Exception("Invalid field type");
                                    }
                                }

                                break;
                            }
                        }
                    }
                }
            }
            return result;
        }

    }
     * */
}
